import * as echarts from '../../ec-canvas/echarts.min';
import Notify from '@vant/weapp/notify/notify';
const chartsOption = require('../../utils/chartsOption')
import {
  formatDateTimey
} from '../../utils/util'
var barComponent = null
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    barData: {
      xData: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
      yData: [18, 36, 65, 30, 78, 40, 33],
      zData: [1, 6, 6, 3, 9, 4, 3],
      type: 'line'
    },
    ec: {
      lazyLoad: true // 懒加载
    },
    sign: null,
    ispopupShow: false,
    CjstateValue: '',
    CjstateIndex: 0,
    CodingValue: '',
    typeIndex: 1,
    columnsCjState: [{
      id: 0,
      text: '按层级搜索'
    }, {
      id: 1,
      text: '选项一'
    }, {
      id: 2,
      text: '选项二'
    }],
    customStyleSelect: "padding:0 0 0 20rpx;height: 100%;width:100%;color:#808285;",
    startDate: formatDateTimey(new Date()),
    endDate: formatDateTimey(new Date()),
    rzstartDate: formatDateTimey(new Date()),
    rzendDate: formatDateTimey(new Date()),
    pageNoDIY: '',
    pageData: {
      pageNo: 1,
      pageSize: 5
    },
    total: 1,
    listData: [{
        id: 1,
        goodsName: "1",
        goodsNo: "主动触发",
        goodsCount: "2024-03-11 00:00:00",
        ischecked: false
      },
      {
        id: 2,
        goodsName: "2",
        goodsNo: "后退",
        goodsCount: "2024-03-11 00:00:00",
        ischecked: false
      },
      {
        id: 3,
        goodsName: "3",
        goodsNo: "启动成功",
        goodsCount: "2024-03-11 00:00:00",
        ischecked: false
      },
      {
        id: 4,
        goodsName: "4",
        goodsNo: "启动异常",
        goodsCount: "2024-03-11 00:00:00",
        ischecked: false
      },
      {
        id: 5,
        goodsName: "5",
        goodsNo: "启动成功",
        goodsCount: "2024-03-11 00:00:00",
        ischecked: false
      },
    ],

    pageNoDIYCz: '',
    pageDataCz: {
      pageNo: 1,
      pageSize: 5
    },
    totalCz: 1,
    listDataCz: [{
        id: 1,
        goodsNo: "1",
        goodsCoding: "GF01",
        goodsfacilityCount: "8001001",
        goodsCount: "启用",
        goodsEnd: "成功",
        ischecked: false
      },
      {
        id: 2,
        goodsNo: "2",
        goodsCoding: "GF02",
        goodsfacilityCount: "8001002",
        goodsCount: "启用",
        goodsEnd: "成功",
        ischecked: false
      },
      {
        id: 3,
        goodsNo: "3",
        goodsCoding: "GF03",
        goodsfacilityCount: "8001003",
        goodsCount: "启用",
        goodsEnd: "成功",
        ischecked: false
      },
    ],
  },
  DatestartChange(e) {
    let that = this
    if (that.data.typeIndex == 1) {
      this.setData({
        startDate: e.detail.value
      })
    } else {
      this.setData({
        rzstartDate: e.detail.value
      })
    }
  },
  newDate(old,val){
    if(new Date(old).getTime()>new Date(val).getTime()){
      return false
    }else{
      return true
    }
  },
  DateendChange(e) {
    let that = this
    console.log(that.newDate(that.data.startDate,e.detail.value));
    if (that.data.typeIndex == 1) {
      if(!that.newDate(that.data.startDate,e.detail.value)){
        Notify({
          type: 'warning',
          background: '#48C1BF',
          message: '结束时间小于开始时间'
        });
        return false
      }
      that.setData({
        endDate: e.detail.value
      })
    } else if (that.data.typeIndex == 2) {
      if(!that.newDate(that.data.rzstartDate,e.detail.value)){
        Notify({
          type: 'warning',
          background: '#48C1BF',
          message: '结束时间小于开始时间'
        });
        return false
      }
      that.setData({
        rzendDate: e.detail.value
      })
    } 
  },

  onClickInput(e) {
    let that = this
    that.setData({
      sign: e.currentTarget.dataset.sign,
      ispopupShow: true
    })
  },
  cancelpicker(e) {
    let that = this
    that.setData({
      ispopupShow: false
    })
    setTimeout(() => {
      that.setData({
        sign: null
      })
    }, 200)
  },
  //层级
  onConfirmCjState(event) {
    let that = this
    that.setData({
      CjstateIndex: event.detail.index,
      CjstateValue: event.detail.value.id == 0 ? '' : event.detail.value.text,
      ispopupShow: false
    })
  },
  //设备编码
  onCodingChange(e) {
    let that = this
    that.setData({
      CodingValue: e.detail
    })
  },
  Querystation() {
    let that = this
    let queryParams = {}
    queryParams.CjstateValue = that.data.CjstateValue
    queryParams.CodingValue = that.data.CodingValue
    console.log('设备查询', queryParams);
  },
  Querydate() {
    let that = this
    let queryParams = {}
    if (that.data.typeIndex == 1) {
      queryParams.startDate = that.data.startDate
      queryParams.endDate = that.data.endDate
    } else if (that.data.typeIndex == 2) {
      queryParams.rzstartDate = that.data.rzstartDate
      queryParams.rzendDate = that.data.rzendDate
    }
    console.log('日期查询', queryParams);
  },
  switchover(e) {
    let that = this
    that.setData({
      typeIndex: e.currentTarget.dataset.type
    })
    if (e.currentTarget.dataset.type == 1) {
      that.initChart(that.data.barData)
    }
  },
  switctablehover(e) {
    let that = this
    that.setData({
      tableIndex: e.currentTarget.dataset.type
    })
  },
  
  pageNoDIYchange(e) {
    let that = this
    console.log(e.detail);
    if (e.detail != '') {
      let reg = /^[1-9]\d*$/;
      if (!reg.test(e.detail)) {
        e.detail = 1
      }
    }
    that.setData({
      pageNoDIY: e.detail
    })
  },
  prev() {
    let that = this
    if (that.data.pageData.pageNo == 1) {
      Notify({
        type: 'warning',
        background: '#48C1BF',
        message: '已经是第一页'
      });
    }
  },
  next() {
    let that = this
    if (that.data.pageData.pageNo == that.data.total) {
      Notify({
        type: 'warning',
        background: '#48C1BF',
        message: '已经是最后一页'
      });
    }
  },
  pageNoGo() {
    if (this.data.pageNoDIY > this.data.total) {
      Notify({
        type: 'warning',
        background: '#48C1BF',
        message: '输入页码大于总页码'
      });
    }
    if (this.data.pageNoDIY < 1) {
      Notify({
        type: 'warning',
        background: '#48C1BF',
        message: '输入页码最小为1'
      });
    }
  },
  onView(e){
    console.log('查看',e.currentTarget.dataset.item);
    wx.navigateTo({
      url: '/pages/logdetails/logdetails?Value='+JSON.stringify(e.currentTarget.dataset.item),
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  //图表
  initChart(barData) {
    // 绑定组件
    this.barComponent = this.selectComponent("#mychart-dom-bar");
    // 初始化柱状图
    this.barComponent.init((canvas, width, height, dpr) => {
      // 初始化图表
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // 解决模糊显示问题
      })
      // 开发中根据从后端获取barData数据,动态更新图表
      chart.setOption(chartsOption.barOption(barData))
      return chart
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.initChart(this.data.barData)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})